package com.qf.service.impl;

import com.qf.cache.ExecFiltersCache;
import com.qf.filters.FiltersChain;
import com.qf.service.DataFilterManager;
import com.qf.service.SendReportService;
import com.qianfeng.smsplatform.common.constants.CacheConstants;
import com.qianfeng.smsplatform.common.constants.InterfaceExceptionDict;
import com.qianfeng.smsplatform.common.model.Standard_Report;
import com.qianfeng.smsplatform.common.model.Standard_Submit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/*
 * @Data: 2021/7/17 15:33
 * */
@Service
public class DataFilterManagerImpCache implements DataFilterManager {

    private SendReportService sendReportService;
    @Autowired
    public void setSendReportService(SendReportService sendReportService) {
        this.sendReportService = sendReportService;
    }

    private Map<String,FiltersChain> map;
    @Autowired
    public void setMap(Map<String, FiltersChain> map) {
        this.map = map;
    }

    private ExecFiltersCache filterCache;
    @Autowired
    public void setFilterCache(ExecFiltersCache filterCache) {
        this.filterCache = filterCache;
    }


    @Override
    public void dealSms(Standard_Submit standard_submit) {
//      获取缓存中所有的策略
        List<String> filter = filterCache.getFilter();
        if(filter==null||filter.size()==0){
//            缓存里面没有 则执行所有的策略
            Set<Map.Entry<String, FiltersChain>> entries = map.entrySet();
            for(Map.Entry<String, FiltersChain> fil:entries){
                FiltersChain value = fil.getValue();//单个策略
                boolean b = value.dealSms(standard_submit);
                if(!b){
                    return;
                }
            }
        }else {
            for(String name:filter){
                FiltersChain filtersChain = map.get(name);

                if(filtersChain!=null){
                    boolean result = filtersChain.dealSms(standard_submit);
                    if(!result){
//                     TODO   只要到这里状态一定是失败  将数据发送给客户以及日志中
                      Standard_Report standard_report = new Standard_Report();
                        standard_report.setClientID(standard_submit.getClientID());
                        standard_report.setSrcID(standard_submit.getSrcSequenceId());
                        standard_report.setState(2);
                        standard_report.setErrorCode(standard_submit.getErrorCode());
                        standard_report.setDescription(standard_submit.getDescription());
                        standard_report.setMobile(standard_submit.getDestMobile());
                        standard_report.setSendCount(1);//第几次推送, 是这样的,我们告诉客户结果,中间可能因为网络等原因没推送到,我们需要重试,但是呢又不是无限制重试,我们可以设置一个阈值
                        sendReportService.sendSmsReport(standard_report);

                        sendReportService.sendSmsToLog(standard_submit);

                                return;// 代表拦截了
                    }
                }

            }
        }
//       TODO 所有的过滤器 都正常执行完了  在这里添加限流\
    }
}
